package com.yuw.control.userinfo;

import com.alibaba.fastjson.JSON;
import com.yuw.bean.userinfo.UserInfoBean;
import com.yuw.model.userinfo.ModelUserInfo;
import com.yuw.utils.MyUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "ServletUserinfoQuery", value = "/ServletUserinfoQuery")
public class ServletUserinfoQuery extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 接收客户端发送的表单数据
        UserInfoBean userInfoBean = MyUtils.convertRequest2Bean(request, UserInfoBean.class);
        // 调用model层进行业务逻辑处理
        List<UserInfoBean> lstUsers = MyUtils.getNewInstance(ModelUserInfo.class).queryUserinfoByParams(userInfoBean);
        // 将查询结果集发挥ajax引擎
        // 将查询结果集转换为json字符串
        String strJsonUsers = JSON.toJSONStringWithDateFormat(lstUsers, "yyyy-MM-dd HH:mm:ss");
        // 使用response将ajax响应的内容以字节流的形式发回客户端
        PrintWriter printWriter = response.getWriter();
        // 写出数据流
        printWriter.write(strJsonUsers);
        // 清空数据流
        printWriter.flush();
    }
}
